package com.lanou3g.mynewstest.module.news.headline;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.NewsDetailBeans;
import com.lanou3g.mynewstest.beans.NewsHeadlineSpecialBeans;
import com.lanou3g.mynewstest.beans.NewsPhotoSetBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/13.
 */
public class SpecialAdapter extends BaseExpandableListAdapter {
    private NewsHeadlineSpecialBeans data;
    private Context context;
    private static final String URL_HEAD = "http://c.m.163.com/nc/article/";
    private static final String URL_FOOT = "/full.html";
    private NewsDetailBeans beans;

    public SpecialAdapter(NewsHeadlineSpecialBeans data, Context context) {
        this.data = data;
        this.context = context;

    }

    @Override
    public int getGroupCount() {
        return data.getSpecial().getTopics().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.getSpecial().getTopics().get(groupPosition).getDocs() != null ? data.getSpecial().getTopics().get(groupPosition).getDocs().size() : 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.getSpecial().getTopics().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_special_group, null);
            holder = new GroupViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        holder.textView.setText(data.getSpecial().getTopics().get(groupPosition).getIndex() + "/" + data.getSpecial().getTopics().size()
                + " " + data.getSpecial().getTopics().get(groupPosition).getTname());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolderThree holerThree = null;
        ChildViewHolderNormal holderNormal = null;
        View viewNormal = null;
        View viewThree = null;
        boolean flag = data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra() == null;
        if (viewNormal == null || viewThree == null) {
            if (flag) {
                viewNormal = LayoutInflater.from(context).inflate(R.layout.item_headline_normal, parent, false);
                viewNormal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goToDetail(groupPosition,childPosition);
                    }
                });
                holderNormal = new ChildViewHolderNormal(viewNormal);
                viewNormal.setTag(holderNormal);
            } else if (data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().size() > 0) {
                viewThree = LayoutInflater.from(context).inflate(R.layout.item_headline_threepic, parent, false);
                holerThree = new ChildViewHolderThree(viewThree);
                viewThree.setTag(holerThree);
            }
        } else {
            if (flag) {
                holderNormal = (ChildViewHolderNormal) viewNormal.getTag();
            } else if (data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().size() > 0) {
                holerThree = (ChildViewHolderThree) viewThree.getTag();
            }
        }
        if (flag) {
            holderNormal.digest.setText(data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getDigest());
            holderNormal.replyCount.setText("" + data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getReplyCount());
            holderNormal.title.setText(data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());

            MyRequestQueue.setNetImage(holderNormal.imageView, data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
            return viewNormal;
        } else if (data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().size() > 0) {
            holerThree.replyCount.setText("" + data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getReplyCount());
            holerThree.title.setText(data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getTitle());
            MyRequestQueue.setNetImage(holerThree.imageView1, data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgsrc());
            MyRequestQueue.setNetImage(holerThree.imageView2, data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(0).getImgsrc());
            MyRequestQueue.setNetImage(holerThree.imageView3, data.getSpecial().getTopics().get(groupPosition).getDocs().get(childPosition).getImgextra().get(1).getImgsrc());
            return viewThree;
        }
        return null;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView textView;

        public GroupViewHolder(View view) {
            textView = (TextView) view.findViewById(R.id.item_special_group_tv);
        }
    }

    class ChildViewHolderNormal {
        TextView title, digest, replyCount;
        NetworkImageView imageView;
        ImageView special;


        public ChildViewHolderNormal(View itemView) {
            title = (TextView) itemView.findViewById(R.id.item_headline_normal_title);
            digest = (TextView) itemView.findViewById(R.id.item_headline_normal_digest);
            replyCount = (TextView) itemView.findViewById(R.id.item_headline_normal_replyCount);
            imageView = (NetworkImageView) itemView.findViewById(R.id.item_headline_normal_iv);
            special = (ImageView) itemView.findViewById(R.id.item_headline_normal_type_iv);
        }
    }

    class ChildViewHolderThree {
        TextView title, replyCount;
        NetworkImageView imageView1, imageView2, imageView3;

        public ChildViewHolderThree(View itemView) {
            title = (TextView) itemView.findViewById(R.id.item_headline_threepic_title);
            replyCount = (TextView) itemView.findViewById(R.id.item_headline_threepic_replyCount);
            imageView1 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv1);
            imageView2 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv2);
            imageView3 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv3);
        }
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    private void goToDetail(final int group, final int child) {
        String url = data.getSpecial().getTopics().get(group).getDocs().get(child).getDocid();
        url = URL_HEAD + url + URL_FOOT;
        StringRequestUtf request = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        response = response.replace(data.getSpecial().getTopics().get(group).getDocs().get(child).getDocid(), "postID");
                        beans = gson.fromJson(response, NewsDetailBeans.class);

                        DetailActivity.goToDetailActivity(context, beans);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        MyRequestQueue.addRequest(request);
    }
}
